package cn.edu.nuc.zyb.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import cn.edu.nuc.zyb.io.Io;
import cn.edu.nuc.zyb.model.Admin;
import cn.edu.nuc.zyb.model.Cart;
import cn.edu.nuc.zyb.model.Goods;
import cn.edu.nuc.zyb.model.User;

public class UserClient {

	Socket socket;

	public UserClient() {
		try {
			socket = new Socket("127.0.0.1", 4000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * public UserClient(String username, int password) { this.username =
	 * username; this.password = password; try { socket = new
	 * Socket("127.0.0.1", 4000); } catch (UnknownHostException e) { // TODO
	 * Auto-generated catch block e.printStackTrace(); } catch (IOException e) {
	 * // TODO Auto-generated catch block e.printStackTrace(); }
	 * 
	 * }
	 */

	public boolean sendMsg(String username, int password) {
		try {

			// 发送数据
			DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
			StringBuilder buf = new StringBuilder();
			String msg = "login," + username + "," + password;
			System.out.println(msg);
			dos.writeUTF(msg);

		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;

	}

	public boolean acceptMsg() {
		// InputStream is;
		// DataInputStream dis;
		try {
			InputStream is = socket.getInputStream();
			DataInputStream dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;

	}

	public void listGoods() {
		String msg = "listgoods,";
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List<Goods> acceptListGoods() {
		try {

			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			List<Goods> list = (List<Goods>) oos.readObject();
			System.out.println(list.size());
			return list;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void searchGoods(String goodsName) {
		String msg = "searchgoods," + goodsName;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public Goods acceptSearchGoods() {
		try {
			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			List<Goods> list = (ArrayList<Goods>) oos.readObject();
			System.out.println(list.size());
			return list.get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;

	}

	public void addUser(String username, int password, String name, int age) {
		String msg = "regist," + username + "," + password + "," + name + "," + age;
		// PrintWriter writer = null;

		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public void lookGoods(String goodname) {
		String msg = "lookgoods," + goodname;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public Goods acceptLookGoods() {
		// 接收到服务器返回来的消息
		try {
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			return (Goods) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void addCart(int id, String name, float price, int num, String un) {
		String msg = "addcart," + id + "," + name + "," + price + "," + num + "," + un;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void listCart(String name) {
		String msg = "listcart," + name;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public List<Cart> acceptAddCart() {
		try {
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			return (List<Cart>) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public void settleAccounts(String name, float money) {
		String msg = "settleaccounts," + name + "," + money;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void changeNum(String name) {
		String msg = "changenum," + name;
		try {
			OutputStream os = socket.getOutputStream();
			DataOutputStream dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public List<Goods> acceptChangeNum(){
		try {
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			return (List<Goods>) ois.readObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
